import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class LC128 {
    public int longestConsecutive(int[] nums) {
        if(nums.length == 0){
            return 0;
        }
        int ans = 1;
        Set<Integer> hash = new HashSet<>();
        for(Integer e : nums){
            hash.add(e);
        }

        for(Integer e : hash){
            int tmp = 1;
            if(hash.contains(e + 1)){
                tmp++;
                while(hash.contains(e + 1)){
                    tmp++;
                }
                ans = Math.max(ans, tmp);
            }
        }
        return ans;

    }

    //排序
    public int longestConsecutive1(int[] nums) {
        if(nums.length == 0){
            return 0;
        }
        int i = 0;
        int j = 0;
        Arrays.sort(nums);
        int ans = 0;
        int count = 0;
        for(j = 1; j < nums.length; j++){
            if(nums[j] == nums[j - 1]){
                count++;
            }
            if(nums[j] - nums[j - 1] > 1){
                ans = Math.max(ans, j - i - count);
                count = 0;
                i = j;
            }
        }
        ans = Math.max(ans, j - i - count);
        return ans;
    }
}
